# 1.7 Romberg求积法
import math


def fx(x):
    return math.sqrt(x)


a = 1
b = 9
e = 0.01
n = 1
t = [[0 for col in range(100)] for row in range(100)]

t[0][0] = (b - a) / 2 * (fx(a) + fx(b))
k = 1
while (1):
    tmp1 = (b - a) / 2 ** (k - 1)
    tmp2 = 0
    for i in range(1, 2 ** k):
        tmp2 += fx(a + (2 * i - 1) * (b - a) / 2 ** k)
    t[0][k] = 0.5 * (t[0][k - 1] + tmp1*tmp2)
    for m in range(1, k + 1):
        tmp1 = 4 ** m * t[m - 1][k - m + 1] - t[m - 1][k - m]
        t[m][k - m] = tmp1 / (4 ** m - 1)
    # print(t[k][0], t[k - 1][0])
    if (abs(t[k][0] - t[k - 1][0]) < e):
        print("t=%.4f" % t[k][0])
        break
    k += 1
